<?php
/**
 * Created by PhpStorm.
 * User: soosoogoo
 * Date: 5/8
 * Time: 22:44
 * Desc: Http 监听事件
 */

namespace Listen;


use ZScript\APP;
use ZScript\Listen\WebSocket as ZScriptWebSocket;

class WebSocket extends ZScriptWebSocket
{

    /**
     * 进程启动
     * @param $server
     * @param $worker_id
     * @author soosoogoo
     * ------------------------------------------------------------------
     */
    public function onWorkerStart($server, $worker_id)
    {
        parent::onWorkerStart($server, $worker_id);

    }

    /**
     * explain
     * @param \swoole_websocket_server $svr
     * @param \swoole_http_request $req
     * @author soosoogoo
     * ------------------------------------------------------------------
     */
    public function onOpen(\swoole_websocket_server $svr, \swoole_http_request $req)
    {
        // TODO: Implement onOpen() method.
    }


    /**
     * ------------------------------------------------------------------
     * 客户端消息
     * @param \swoole_server $server
     * @param \swoole_websocket_frame $fram
     * @return bool
     * @author soosoogoo
     * ------------------------------------------------------------------
     */
    public function onMessage(\swoole_server $server, \swoole_websocket_frame $fram)
    {
        // TODO: Implement onMessage() method.
        if (!$fram->data) {
            echo "error params \n";
            return false;
        }

        //判断是否是pingpong
        if (Ping::checkPingPong($fram->fd, $fram->data)) {
            return true;
        }

        $params = json_decode($fram->data, true);

        //判断提交参数
        if (!isset($params['action']) || !$params['action']
            || !isset($params['data']) || !$params['data']) {

            echo "error params \n";
            return false;
        }

        //设置参数
        WSRequest::setRequest($params['data']);
        WSRequest::setAction($params['action']);
        var_dump($params);
        //设置fram
        Scaffold::setFrame($fram);

        self::doAction($params['action'], $params['data']);

        //清空
        WSRequest::destroy();
        Scaffold::destroy();
    }


    public function doAction($method, $data)
    {
        //action
        $controller = new Topic();

        if (!method_exists($controller, $method)) {
            echo "class error \n";
            return false;
        }

        //执行
        try {
            $affect = $controller->$method($data);

            APP::send(Scaffold::getFrame()->fd, Scaffold::returnHandle($affect));
        } catch (\Exception $e) {

        }
    }

    /**
     * 进程回调
     * @param \swoole_server $server
     * @param int $src_worker_id
     * @param $message
     * @author soosoogoo
     * @return
     * ------------------------------------------------------------------
     */
    public function doPipeMessage(\swoole_server $server, int $src_worker_id, $message)
    {
        list($params, $action) = \ZScript\Pack\ProcessPack::unpack($message);

        list($controller, $method) = $action;

        return $controller::$method(...$params);
    }


    /**
     * 异步任务
     * @param \swoole_server $serv
     * @param int $task_id
     * @param int $src_worker_id
     * @param $data
     * @author soosoogoo
     * @return
     * ------------------------------------------------------------------
     */
    public function onTask(\swoole_server $serv, int $task_id, int $src_worker_id, $data)
    {
        parent::onTask($serv, $task_id, $src_worker_id, $data); // TODO: Change the autogenerated stub

        // 解包
        list($params, $action) = \ZScript\Pack\ProcessPack::unpack($data);

        list($controller, $method) = $action;

        // 故意不检查类下面方法是否存在, 开发自己清楚就行 new 还是 静态 都可以
        $affect = $controller::$method(...$params);

        if ($affect) {
            return $affect;
        }
    }

    /**
     * 用户连接关闭回调
     * @param \swoole_server $server
     * @param int $fd
     * @param int $reactorId
     * @author soosoogoo
     * ------------------------------------------------------------------
     */
    public function onClose(\swoole_server $server, int $fd, int $reactorId)
    {
        parent::onClose($server, $fd, $reactorId); // TODO: Change the autogenerated stub
    }

}